//
//  UIBarButtonItem+Extension.swift
//  JKWeibo
//
//  Created by albert on 16/5/12.
//  Copyright © 2016年 albert. All rights reserved.
//  UIBarButtonItem分类

import UIKit

extension UIBarButtonItem {
    
    // 1.用于快速创建一个对象
    // 2.依赖于指定构造方法
    convenience init(imageName: String?, target: AnyObject?, action: Selector) {
        
        //self.init()
        
        // 1.创建按钮
        let btn = UIButton(type: UIButtonType.Custom)
        
        // 2.设置图片
        if let imgName = imageName {
            btn.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
            btn.setImage(UIImage(named: imgName + "_highlighted"), forState: UIControlState.Highlighted)
        }
        
        // 3.设置尺寸
        btn.sizeToFit()
        
        // 4.添加点击事件
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        
        // 5.依赖于customView的构造方法
        self.init(customView: btn)
    }
}